<template>
  <div style="margin-left:15px">
    <el-row style="margin-top:15px;margin-left:3px">
      <el-col :span="12">
        <el-button-group>
          <el-button type="primary" @click="addList">新增</el-button>
          <el-button type="danger">高级搜索</el-button>
        </el-button-group>
      </el-col>
      <el-col :span="12">
        <div>
          <el-input placeholder="请输入内容" v-model="input" class="input-with-select">
            <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
          </el-input>
        </div>
      </el-col>
    </el-row>
    <!-- table -->
    <div style="margin-top:15px">
      <el-table :data="this.list" border style="width: 100%">
        <el-table-column fixed type="index" label="#" width="50"></el-table-column>
        <el-table-column prop="title" label="标题" width="150"></el-table-column>
        <el-table-column prop="type" label="分类" width="120"></el-table-column>
        <el-table-column prop="author" label="作者" width="120"></el-table-column>
        <el-table-column prop="time" label="用时" width="120"></el-table-column>
        <el-table-column prop="date" label="发布时间" width="120"></el-table-column>
        <el-table-column fixed="right" label="操作" width="400">
          <template slot-scope="scope">
            <el-button type="info" @click="detail(scope.row)">详情</el-button>
            <el-button type="warning" @click="putList(scope.row)">修改</el-button>
            <el-button type="danger" @click="deleteList(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="block" style="margin-top:15px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5,10,15,20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
    <!-- 表单 -->
    <el-drawer title="我是标题" :visible.sync="drawer" :with-header="false" size="70%">
      <div style="padding: 15px">
        <el-page-header @back="goBack" title="返回"></el-page-header>
      </div>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="标题">
          <el-input :disabled="isAdd" v-model="form.title"></el-input>
        </el-form-item>
        <div>
          <el-form-item label="分类" class="flot">
            <el-select :disabled="isAdd" v-model="form.type" placeholder="请选择类别">
              <el-option
                v-for="(item,index) in classify"
                :label="item.name"
                :key="index"
                :value="item.name"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="用时" class="flot">
            <el-select :disabled="isAdd" v-model="form.time" placeholder="请选择类别">
              <el-option label="一分钟" value="1"></el-option>
              <el-option label="三分钟" value="3"></el-option>
              <el-option label="五分钟" value="5"></el-option>
              <el-option label="十分钟" value="10"></el-option>
            </el-select>
          </el-form-item>
        </div>
        <el-form-item label="作者" style="clear: both;">
          <el-input :disabled="isAdd" value="situ"></el-input>
        </el-form-item>
        <mavonEditor  v-model="form.content" style="max-height:300px;overflow-y:scroll;"></mavonEditor>
        <el-form-item label="即时配送">
          <el-switch :disabled="isAdd" v-model="form.delivery"></el-switch>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">确定</el-button>
          <el-button @click="addList">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import { mavonEditor } from "mavon-editor";
import { mapActions, mapState, mapMutations } from "vuex";
import "mavon-editor/dist/css/index.css";
export default {
  layout: "main",
  data() {
    return {
      form: {},
      input: "",
      drawer: false,
      isAdd:false
    };
  },
  methods: {
    ...mapMutations("msbd", ["GET_CURRNNTPAGE", "GET_PAGESIZE"]),
    ...mapActions("msbd", [
      "getMsbdListApi",
      "postMsbdListApi",
      "deleteMsbdListApi",
      "updateMsbdListApi"
    ]),
    async handleSizeChange(val) {
      this.GET_PAGESIZE(val);
      if (this.input) {
        await this.getMsbdListApi({ query: this.input });
      } else {
        await this.getMsbdListApi();
      }
    },
    async handleCurrentChange(val) {
      this.GET_CURRNNTPAGE(val);
      if (this.input) {
        await this.getMsbdListApi({ query: this.input });
      } else {
        await this.getMsbdListApi();
      }
    },
    handleClick(row) {
      console.log(row);
    },
    goBack() {
      this.drawer = !this.drawer;
    },
    // 新增
    async onSubmit() {
      if (!this.form.id) {
        await this.postMsbdListApi(this.form);
      } else {
        console.log(123);
        await this.updateMsbdListApi(this.form);
      }
      this.drawer = !this.drawer;
      this.form = {};
    },
    // 删除
    async deleteList(id) {
      await this.deleteMsbdListApi({ id });
    },
    // 修改
    putList(row) {
      this.drawer = !this.drawer;
      this.form = { ...row };
    },
    // 搜索
    search() {
      if (!this.input) return;
      this.getMsbdListApi({ query: this.input });
    },
    // 详情
    detail(row) {
      this.drawer = !this.drawer;
      this.isAdd=!this.isAdd
      this.form = { ...row };
    },
    // 关闭抽屉
    addList() {
      this.drawer = !this.drawer;
    }
  },
  components: {
    mavonEditor
  },
  computed: {
    ...mapState("msbd", [
      "list",
      "classify",
      "pageSize",
      "currentPage",
      "total"
    ])
  },
  mounted() {
    this.getMsbdListApi();
  }
};
</script>

<style scoped>
.flot {
  float: left;
}
</style>